package com.sinosoft.dsp.platform.manager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

/**
 * LocaleManager负责保持当前应用的语言环境。 <br>
 * 应用的语言环境可以更改，应用当前支持英语，汉语。
 * 
 * @since 1.0 2009-11-20
 * @author 潘巍（Peter Pan）
 * 
 */
@ManagedBean(name = "localeManager")
@SessionScoped
public class LocaleManager implements Serializable {

	private static final long serialVersionUID = 4954778934959462828L;

	private static final String SEPARATOR = "_";

	// 当前语言
	private String currentLanguage;
	// 当前国家
	private String currentCountry;
	// 当前local
	private Locale usedLocale;

	// 供选择的语言类型
	private ArrayList<SelectItem> AVAILABLE_LOCALES;

	/**
	 * 创建LocalBean的一个实例。默认的语言类型由初始请求指定。
	 */
	@PostConstruct
	public void init() {
		// 得到默认语言类型
		usedLocale = FacesContext.getCurrentInstance().getViewRoot()
				.getLocale();
		currentLanguage = usedLocale.getLanguage();
		currentCountry = usedLocale.getCountry();

		// 提供的语言列表
		AVAILABLE_LOCALES = new ArrayList<SelectItem>();
		AVAILABLE_LOCALES.add(new SelectItem("en", "English"));
		AVAILABLE_LOCALES.add(new SelectItem("zh_CN", "简体中文"));
	}
	
	@PreDestroy
	public void destroy() {

	}

	/**
	 * 更改根视图的语言类型
	 * 
	 * @param event
	 *            jsf value change event.
	 */
	public void changeLanguage(ValueChangeEvent event) {
		FacesContext ctx = FacesContext.getCurrentInstance();
		// 得到当前本地根视图语言类型
		Locale locale = ctx.getViewRoot().getLocale();
		String newLanguage = (String) event.getNewValue();
		// 判断合适的语言类型
		SelectItem si;
		for (Iterator<SelectItem> iter = AVAILABLE_LOCALES.iterator(); iter
				.hasNext();) {
			si = iter.next();
			if (si.getValue().equals(newLanguage)) {
				if (newLanguage.indexOf(SEPARATOR) > 0) {
					String[] lc = newLanguage.split(SEPARATOR);
					currentLanguage = lc[0];
					currentCountry = lc[1];
				} else {
					currentLanguage = newLanguage;
					currentCountry = "";
				}
				break;
			}
		}
		// 最后更改根视图语言类型
		if (!currentLanguage.equals(locale.getLanguage())) {
			usedLocale = new Locale(currentLanguage, currentCountry);
			ctx.getViewRoot().setLocale(usedLocale);
		}
	}

	public String getCurrentCountry() {
		return currentCountry;
	}

	public void setCurrentCountry(String currentCountry) {
		this.currentCountry = currentCountry;
	}

	public void setUsedLocale(Locale usedLocale) {
		this.usedLocale = usedLocale;
	}

	public String getCurrentLanguage() {
		return currentLanguage;
	}

	public void setCurrentLanguage(String currentLanguage) {
		this.currentLanguage = currentLanguage;
	}

	public ArrayList<SelectItem> getAvailableLocales() {
		return AVAILABLE_LOCALES;
	}

	public Locale getUsedLocale() {
		return usedLocale;
	}

}
